/*
作者：邬畏畏
部门：IsoftStone CBG
说明：基于JQuery
*/

var wList=function(p,options){
	this.options = options;
	this.p=$(p);/*Jquery对象*/
	this.parent=p;
	this.url = options.url;
	this.table = document.createElement("table");
	this.table.setAttribute("cellpadding","2");
	this.table.setAttribute("cellspacing","0");
	this.table.setAttribute("id",(this.options.comid||"wList"));
	this.table.setAttribute("class",(this.options.classname||"wList"));
	this.p[0].appendChild(this.table);
	/*this.table.style.display="none";*/
	
	this.maskdiv=document.createElement("div");
	this.maskdiv.innerHTML="&nbsp;正在载入数据....";
	this.maskdiv.style.position="absolute";
	this.maskdiv.style.border="1px solid #ccc";
	this.maskdiv.style.backgroundColor="#eee";
	this.maskdiv.style.width="130px";
	this.maskdiv.style.height="30px";
	this.maskdiv.style.lineHeight="30px";
	this.maskdiv.style.left=(this.table.offsetLeft+100)+"px";
	this.maskdiv.style.top=(this.p.offset().top+50)+"px";
	this.p[0].appendChild(this.maskdiv);
	this.maskdiv.style.display="none";
	
	this.pagediv = document.createElement("div");
	this.pagediv.id="pagediv";
	this.pagediv.innerHTML="";
	this.pagediv.style.fontWeight="bold";
	this.pagediv.style.paddingRight="5px";
	this.pagediv.style.color="#bbb";
	this.p[0].appendChild(this.pagediv);
	this.autowidth = options.autowidth||false;
	this.checkbox = options.checkbox||false;
	this.isCheck=false; /*复选框是否全部选中*/
	this.checkValues=[];/*存储复选框选中的值和主键，格式：{key:key,check:ture|false,row:当前行} --注：外界可以访问*/
	this.backCheckValues=[];/*存储后端JSON中的check数组值*/
	this.lastCheckIndex=-1;/* 最后选中复选框在checkValues中的索引  --注：外界可以访问 */
	this.haveNo = options.haveNo||false;
	this.No=1; /*序号列，从1开始*/
	this.backJSON={}; /* 存储后端传来的所有数据JSON */
	this.options.showkey=true;
	this.checkAllOnce = false;/*是否已经创建了全选的事件,为了防止多次全选创建事件,为true表示已经创建过事件 */
	this.checkformatter = options.checkformatter||undefined;/*自定义函数，决定当前行复选框是否选中。函数返回true表示选中，返回false表示不选 */
	
	if(options.autowidth)
	{
		this.table.width="100%";
	}
	this.rowcount = 0;
	this.pageindex=1;/*分页索引，从1开始,从第二页开始pageindex是下一页的索引*/
	this.curPageIndex=1;/*存储当前页的页码,用于配合this.existPageIndex*/
	this.pageindexold=0;
	this.pagecnt = 5; /*分页时候显示的页的个数*/
	this.pageBase = 0;/*分页的页码起始数，如果页数很多，1234...10,当单击第二页时则变成2345...10*/
	this.pageDirect="prev";/*值为："next"|"prev" ,存储翻页顺序,当单击下一页时设置为"next" ,当单击上一页时设置为"prev" ;显示当前页用到此值作为判断 */
	this.pageFE="front";/*值为："front"|"end",表示当前页是第一页还是最后一页*/
	
	this.existPageIndex=[1]; /*存储已经翻页的索引,索引从1开始，此值在initCheckValues()和ajaxData()函数翻页时中使用*/
	var columns = this.options.columns;
	
	
	if(this.options!=undefined)/*添加header*/
	{
		var colcnt = options.columns.length;
		var d = this.table;
		var i;
		
		if (this.isLowChrome()) 
		{
			row = d.insertRow(-1);
			if(this.options.op!=undefined&&this.options.op.buttons!=undefined)/*如果有buttons列（即："操作" 列）*/
			{
				col = row.insertCell();
				col.id="tdheader"+colcnt;
				col.className="tdheader";
				col.style.width=this.options.op.width+"px";
				col.innerHTML = '<div id="header'+i+'" class="header'+i+'">'+this.options.op.caption+'</div>';			
			}
			for(i=colcnt-1;i>=0;i--){
				var col = row.insertCell();
				if(columns[i].hidden!=undefined&&columns[i].hidden==true)/*是否是隐藏列*/
				{
					col.className="hidden";
					col.style.display="none";
				}				
				col.id="tdheader"+i;
				col.className=col.className + " tdheader";
				col.style.height= (this.options.headerheight||30)+"px";
				if(columns[i].width!=undefined)
				{col.style.width=columns[i].width+"px";}
				col.innerHTML = '<div id="header'+i+'" class="header'+i+'">'+columns[i].caption+'</div>';
			}/*end for*/
			this.addNo(row, true);
			this.addCheckBox(row,true);
		} 
		else
		{
			row = d.insertRow(-1);
			this.addCheckBox(row,true);
			this.addNo(row, true);
			for(i=0;i<colcnt;i++){
				var col = row.insertCell();
				if(columns[i].hidden!=undefined&&columns[i].hidden==true)/*是否是隐藏列*/
				{
					col.className="hidden";
					col.style.display="none";
				}
				col.id="tdheader"+i;
				col.className= col.className + " tdheader";
				col.style.height= (this.options.headerheight||30)+"px";
				if(columns[i].width!=undefined)
				{col.style.width=columns[i].width+"px";}
				col.innerHTML = '<div id="header'+i+'" class="header'+i+'">'+columns[i].caption+'</div>';
				
			}/*end for*/
			if(this.options.op!=undefined&&this.options.op.buttons!=undefined)/*如果没有buttons列（即："操作" 列）*/
			{
				col = row.insertCell();
				col.id="tdheader"+i;
				col.className="tdheader";
				col.style.width=this.options.op.width+"px";
				col.innerHTML = '<div id="header'+i+'" class="header'+i+'">'+this.options.op.caption+'</div>';
			}
		}
		/*增加合并表头。*/
		this.addMultiHead();
	}
	
}/*end wList 构造*/

wList.prototype.addCheckBox=function(row,isHeader,rowIndex){
	var col;
	if(this.checkbox)
	{
		col = row.insertCell();
		col.style.width="25px";
		if(isHeader!=undefined&&isHeader)
		{
			col.innerHTML='<input id="wMainCK" type="checkbox" name="wcheck" value="0" class="wMainCK"/>';
		}
		else
		{
			if(rowIndex!=undefined&&arguments.length==3)
			{
				col.innerHTML='<input type="checkbox" name="wcheck" value="0" class="wcheck" id="wcheck'+rowIndex+'"/>';
			}
		}
	}
}

wList.prototype.addNo = function(row,isHeader){
	var col;
	if(this.haveNo)
	{
		col = row.insertCell();
		col.style.width="40px";
		if(isHeader!=undefined&&isHeader)
		{
			col.className="tdheader";
			col.innerHTML = '<div class="headerNo">序号</div>';
		}
		else	
		{
			col.innerHTML='<div class="No">'+this.No+'</div>';
			this.No++;
		}
	}/* endif haveNO */
}

wList.prototype.findKeyForWebkit=function(JSON,ii,keyIndex){/*真对webkit内核找到主键在JSON中的位置**已经不用了2013-12-27***/
	var kIndex = keyIndex;
	var fieldcnt = JSON.rows[ii].length;
	var r;
	if(window.navigator.userAgent.indexOf("WebKit") > 0)
	{
		r = fieldcnt - kIndex -1;
		return r;
	}
	else
	{
		return keyIndex;
	}
}

/* 外界调用，new对象后调用此方法 */
wList.prototype.loadData = function(JSON){
	/*this.setData(JSON);用于载入静态数据*/
	this.No=1;
	this.checkValues.length=0;
	if(JSON!=undefined)
	{
		this.setData(JSON);
	}
	else
	{
		this.pagediv.style.display="block";
		this.pageindex = 1;
		this.pageDirect="prev";
		this.ajaxData(1);
	}
}/*end function loadData*/

wList.prototype.setData=function(JSON){
	var i,j,ii,keyIndex=-1;
	this.rowcount = 0;/*统计当前页行数，用于分页*/
	/*
	ii,i,j是循环变量
	ii=数据列表的每行 
	i,j组合起来，判断参数JSON的data中每列的name对应表头中colums的位置，以便使用 insertCell()插入
	i=每一列
	j=每一列，j的值表示是第几列的值对应表头的一个列 
	keyIndex表示主键是在JSON的位置
	*/
	var This = this;
	var table = this.table;
	var row = null;
	var colcnt = this.options.columns.length;
	var columns = this.options.columns;
	var bool=false; /*是否隐藏主键列，false表示隐藏，true表示不显示*/
	var col = null;
	var isNull=false;/*如果后台数据JSON，字段在 columns中没有则isNull=true,否则isNull=false*/
	var fieldcnt =0; /*后端返回JSON的每行列数即字段数*/
	var tmpJSON = JSON;

	JSON = this.exChangeData(JSON);

	if(this.options!=undefined)
	{
	if(this.isLowChrome())
	{
		for(ii=0;ii<JSON.rows.length;ii++) /*-- 每行处理 --*/
		{
			bool=this.options.showkey||false;
			row = table.insertRow(-1);
			
			fieldcnt = JSON.rows[ii].length;
			for(j=0;j<fieldcnt;j++)
			{
				if(JSON.rows[ii][j].name==this.options.keyname)
				{keyIndex=j;}
			}
			
			if(this.options.op!=undefined&&this.options.op.buttons!=undefined)
			{
				this.setOP(JSON,ii,row,keyIndex);
			}
			
			for(i=colcnt-1;i>=0;i--)/* 每一列 */
			{
				isNull=false;
				fieldcnt = JSON.rows[ii].length;
				for(j=0;j<fieldcnt;j++)
				{
					if(columns[i].name==this.options.keyname)
					{/*keyIndex=j;*/}
					try
					{
						if(columns[i].name==JSON.rows[ii][j].name)
						{
							break;
						}
					}
					catch(e)
					{
						isNull=true;
						break;
					}/*end try*/
				}/*end for j */
				if(keyIndex==-1 || bool) /*不是主键并且bool=true(隐藏主键) ，则调用 setLinesData */
				{
					col = row.insertCell();
					this.setLinesData(JSON,col,ii,i,j,keyIndex,isNull);
				}
				else
				{
					bool=true;
				}
	
			}/*end for i --每一列--*/
			this.addNo(row);
			this.addCheckBox(row,false,ii);
			this.rowcount++;/*统计行数*/
			this.initCheckValues(JSON,ii,keyIndex);
			if(typeof(this.checkformatter)!="undefined")/*调用自定义回调函数，达到是否让当前行复选框选中*/
			{
				var paramObj = {};
				paramObj.JSON = tmpJSON;
				paramObj.rowIndex = ii;
				paramObj.key = JSON.rows[ii][keyIndex].text;				
				if(this.checkformatter.call(this,paramObj))/*如果this.checkformatter返回true，当前行设置选中*/
				{
					this.selectCheckBox(ii);
				}
			}			
		}/*end for ii 每一行*/	
	}
	else
	{
		for(ii=0;ii<JSON.rows.length;ii++) /*-- 每行处理 --*/
		{
			bool=this.options.showkey||false;
			row = table.insertRow(-1);
			
			this.addCheckBox(row,false,ii);
			this.addNo(row);

			fieldcnt = JSON.rows[ii].length;
			for(j=0;j<fieldcnt;j++)
			{
				if(JSON.rows[ii][j].name==this.options.keyname)
				{keyIndex=j;}
			}
			
			for(i=0;i<colcnt;i++)/* 每一列 */
			{
				isNull=false;
				fieldcnt = JSON.rows[ii].length;
				for(j=0;j<fieldcnt;j++)
				{
					if(columns[i].name==this.options.keyname)
					{/*keyIndex=j;*/}
					try
					{
						if(columns[i].name==JSON.rows[ii][j].name)
						{
							break;
						}
					}
					catch(e)
					{
						isNull=true;
						break;
					}/*end try*/
				}/*end for j */
				if(keyIndex==-1 || bool) /*不是主键并且bool=true(隐藏主键) ，则调用 setLinesData */
				{
					col = row.insertCell();
					this.setLinesData(JSON,col,ii,i,j,keyIndex,isNull);
				}
				else
				{
					bool=true;
				}
	
			}/*end for i --每一列--*/
			if(this.options.op!=undefined&&this.options.op.buttons!=undefined)
			{
				this.setOP(JSON,ii,row,keyIndex);
			}
			this.rowcount++;/*统计行数*/
			this.initCheckValues(JSON,ii,keyIndex);
			if(typeof(this.checkformatter)!="undefined")/*调用自定义回调函数，达到是否让当前行复选框选中*/
			{
				/*this.checkformatter的回调参数是一个paramObj 包含：后端JSON数据、当前行索引、当前行主键值*/
				var paramObj = {};
				paramObj.JSON = tmpJSON;
				paramObj.rowIndex = ii;
				paramObj.key = JSON.rows[ii][keyIndex].text;
				if(this.checkformatter.call(this,paramObj))/*如果this.checkformatter返回true，当前行设置选中*/
				{
					this.selectCheckBox(ii);
				}
			}
		}/*end for ii 每一行*/
	}/* end if 是否webkit */
	}/* end if 是否定义this.options*/	

	this.setPageRange(JSON.total);
	this.initCheckboxEvent(JSON,keyIndex);
	this.setCheck(tmpJSON);
	this.hiddenColumns();
}/*end function setData*/


/*初始化所有复选框的key主键和选中状态，以便后续操作，需要在setData中循环调用*/
/*
避免this.checkValues[]数组重复push每页的值方法：
在向后翻页和goto页操作中使用this.existPageIndex存储页码，当翻页时判断this.curPageIndex是否在this.existPageIndex里，
就知道this.checkValues[]是否存储过此页的值
*/
wList.prototype.initCheckValues=function(JSON,ii,keyIndex){
	if(keyIndex<0){return;}
	var i=0;
	var len=this.checkValues.length;
	var checkValues = this.checkValues;
	var chkobj = {};
	chkobj.key = JSON.rows[ii][keyIndex].text;
	chkobj.check = false;
	chkobj.row = this.backJSON.rows[ii];
	
	/*
	 existPageIndex数组是用户已经翻页的页码，判断当前页是否不在existPageIndex数组里
	 如过不在就执行this.checkValues.push();
	*/
	for(i=0;i<this.existPageIndex.length-1;i++)
	{
		if(parseInt(this.curPageIndex)==parseInt(this.existPageIndex[i]))
		{
			break;
		}
	}
	if(i>=this.existPageIndex.length-1)
	{
		this.checkValues.push(chkobj);
	}

}/* end function initCheckValues */


/* 对所有的<input checkbox>添加事件 */
wList.prototype.initCheckboxEvent=function(JSON,keyIndex){
	var This=this;
	var rows = "";
	var i=0,checkStart;
	checkStart = 0;
	var len=0;
	if(!this.checkAllOnce)
	{
		$(this.parent + " #wMainCK").click(function(){
			checkStart = parseInt((This.curPageIndex-1)*This.options.pagesize);
			len = parseInt(checkStart)+parseInt(This.options.pagesize);/*如果是最后一个，这里有问题，下面由try{}跳过*/
			/*alert(checkStart + "|" +len);*/
			if(!This.isCheck)/*isCheck存储全选按钮的状态*/
			{
				$(This.parent + " .wcheck").attr("checked","checked");
				if(This.options.keepcheck)/*keepcheck由用户设计*/
				{
					for(i=checkStart;i<len;i++)
					{
						This.lastCheckIndex = i;
						try
						{
							This.checkValues[i].check=true;
						}
						catch(e)
						{
							break;
						}
						
					}
				}
				else
				{
					for(i=checkStart;i<This.rowcount;i++)
					{
						try
						{
							This.checkValues[i].check=true;
						}
						catch(e){;};
					}
				}
				This.isCheck=true;
			}
			else
			{
				$(This.parent + " .wcheck").removeAttr("checked");
				if(This.options.keepcheck)/*keepcheck由用户设计*/
				{
					for(i=checkStart;i<len;i++)
					{
						This.lastCheckIndex = i;
						try
						{
							This.checkValues[i].check=false;
						}
						catch(e)
						{
							break;
						}
					}			
				}
				else
				{
					for(i=checkStart;i<This.rowcount;i++)
					{
						try
						{
							This.checkValues[i].check=false;
						}
						catch(e){;};
					}
				}
				This.isCheck=false;
			}
			This.SetBackCheckValues();
		});/* end click #wMainCK */
		
		this.checkAllOnce=true;
	}
	
	$(this.parent + " .wcheck").click(function(i){

		var key = "",len=0,i=0;

		if(This.haveNo)
		{
			if(this.parentNode.nextSibling.nextSibling.children[0].innerText!=undefined)
			{
				key = this.parentNode.nextSibling.nextSibling.children[0].innerText;
			}
			else
			{
				key = this.parentNode.nextSibling.nextSibling.children[0].textContent;
			}
		}
		else
		{
			if(this.parentNode.nextSibling.nextSibling.children[0].innerText!=undefined)
			{
				key = this.parentNode.nextSibling.children[0].innerText;
			}
			else
			{
				key = this.parentNode.nextSibling.children[0].textContent;
			}
		}
		
		len = This.checkValues.length;
		for(i=0;i<len;i++)
		{
			if(This.checkValues[i].key == key)
			{
				This.lastCheckIndex = i;
				This.checkValues[i].check=this.checked;
				break;
			}
		}
		This.SetBackCheckValues();
	});
	
}/* end function initCheckboxEvent */

wList.prototype.trClickEvent=function(){
	
}
/***********功能：设置列表每行的每一列数据**************/
wList.prototype.setLinesData=function(JSON,col,ii,i,j,keyIndex,isNull){
	/*
	isNull=ture表示此列为空
	isNull=false表示此列不为空
	用户可能没有传递此字段的值
	*/
	var argc_obj={};/*给回掉函数的参数，格式是JSON*/
	var row_text="&nbsp;";
	var columns = this.options.columns;
	var argc="?id=";

	this.formatter = columns[i].formatter; /*如果options里有formatter:function(){return "xxxx";} ,this.formatter指向这个函数，用于使用函数的返回值*/
	
	var colclass="";
	
	if(columns[i].hidden!=undefined&&columns[i].hidden==true)
	{
		col.className="hidden";
		colclass = colclass + " hidden";
		col.style.display="none";
	}
	if(isNull)
	{
		row_text = "&nbsp;";
		if(this.formatter!=undefined)
		{				
			try
			{
			argc_obj.value=row_text;
			argc_obj.rowIndex=ii;
			argc_obj.key=JSON.rows[ii][keyIndex].text;
			}catch(e)
			{;};
			argc_obj.JSON = this.backJSON;
			row_text = this.formatter.call(this,argc_obj);
		}
	}
	else
	{
		row_text = "&nbsp;";

		if(this.formatter==undefined)
		{
			try
			{
			row_text = JSON.rows[ii][j].text;
			}catch(e)
			{;}
		}
		else
		{
			try
			{
			argc_obj.key=JSON.rows[ii][keyIndex].text;
			argc_obj.rowIndex=ii;
			argc_obj.value=JSON.rows[ii][j].text;
			}catch(e)
			{;};
			argc_obj.JSON = this.backJSON;
			row_text = this.formatter.call(this,argc_obj);
		}
	}
	
	try
	{
	if(columns[i].href.indexOf("?")>0){argc="&id=";}
	}catch(e)
	{;}
	col.style.height= (this.options.rowheight||30)+"px";
	if(this.options.optype=="link" || this.options.optype==undefined)/* --设置数据单击后是链接还是弹出层-- */
	{
		if(columns[i].type=="icon")
		{
			/*不用了*/
		}
		else
		{
			if(columns[i].href==undefined)
			{col.innerHTML = '<div id="tddiv'+i+'" class="tddiv tddiv'+i+colclass+'">'+row_text+'</div>';}
			else
			{
				col.innerHTML = '<div id="tddiv'+i+'" class="tddiv tddiv'+i+colclass+'"><a target="'+(columns[i].target||"_self")+'" href="'+columns[i].href+argc+JSON.rows[ii][keyIndex].text+'">'+row_text+'</a></div>';
			}
		}
	}
	else if(this.options.optype=="dialog")/* 弹出层 */
	{
		if(columns[i].type=="icon")
		{
			/*不用了*/
		}
		else
		{
			if(columns[i].callback==undefined)
			{col.innerHTML = '<div id="tddiv'+ii+'_'+i+'" class="tddiv tddiv'+i+colclass+'">'+row_text+'</div>';}
			else
			{
				argc_obj.key=JSON.rows[ii][keyIndex].text;
				argc_obj.value=row_text;
				argc_obj.rowIndex=ii;
				argc_obj.JSON = this.backJSON;
				col.innerHTML = '<div id="tddiv'+ii+'_'+i+'" class="tddiv tddiv'+i+colclass+' callback" style="cursor:pointer">'+row_text+'</div>';
				$(this.parent + ' #tddiv'+ii+'_'+i).click(function(){columns[i].callback.call(this,argc_obj)});
			}
		}
	}/* end if */
	
}/*end function setLinesData*/

/***********功能：设置列表每行的最后一列操作项**************/
wList.prototype.setOP=function(JSON,ii,row,keyIndex){
	var This = this;
	var op = this.options.op;
	var collen = this.options.columns.length;
	var argc="?id=";
	var fieldcnt = JSON.rows[ii].length;
	var btn_text = "";
	var argc_obj={};/*给回掉函数的参数，格式是JSON*/
	var formatter = null;
		
	if(this.options.op!=undefined)/*---- 是否有操作功能 ------*/
	{
		if(this.options.optype=="link" || this.options.optype==undefined)/* --设置数据单击后是链接还是弹出层-- */
		{
			col = row.insertCell();
			var div = '<div id="op'+ii+'" class="op">{0}</div>';
			var link,a = '<a href="{href}" target="{target}">{text}</a>&nbsp;';
			var buttons="";
			for(i=0;i<this.options.op.buttons.length;i++)
			{
				if(this.options.op.buttons[i].href.indexOf("?")>0){argc="&id=";}

				link = a.replace("{href}",this.options.op.buttons[i].href + argc + JSON.rows[ii][keyIndex].text);
				link = link.replace("{target}",(this.options.op.buttons[i].target||'_self'));
				btn_text = this.options.op.buttons[i].text;
				formatter = this.options.op.buttons[i].formatter;
				if(formatter==undefined)
				{
					link = link.replace("{text}",btn_text);
				}
				else
				{
					argc_obj.key=JSON.rows[ii][keyIndex].text;
					argc_obj.value=btn_text;
					argc_obj.rowIndex=ii;
					argc_obj.JSON = this.backJSON;					
					link = link.replace("{text}",formatter.call(this,argc_obj));
				}
				buttons = buttons + link;
			}
			div = div.replace("{0}",buttons);
			col.innerHTML = div;
		}
		else if(this.options.optype=="dialog")/* 弹出层 */
		{
			col = row.insertCell();
			var div = '<div id="op'+ii+'" class="op">{0}</div>';
			var link,a = '<span id="op'+ii+'_{i}" style="cursor:pointer">{text}</span>&nbsp;';
			var buttons="";
			
			for(i=0;i<this.options.op.buttons.length;i++)
			{
				btn_text = this.options.op.buttons[i].text;
				formatter = this.options.op.buttons[i].formatter;
				
				link = a.replace("{i}",i);
				if(formatter==undefined)
				{
					link = link.replace("{text}",btn_text);
				}
				else
				{
					argc_obj.key=JSON.rows[ii][keyIndex].text;
					argc_obj.value=btn_text;
					argc_obj.rowIndex=ii;
					argc_obj.JSON = this.backJSON;
					link = link.replace("{text}",formatter.call(this,argc_obj));
				}
				buttons = buttons + link;
			}
			
			div = div.replace("{0}",buttons);
			col.innerHTML = div;
			
			for(i=0;i<this.options.op.buttons.length;i++)/*给buttons动态添加事件*/
			{
				if(this.options.op.buttons[i].callback!=undefined)
				{
					var span = $(this.parent + " #op" + ii + "_" + i)[0];
					span.onclick=(function(i){
						return function(){
						/*var obj={button:op.buttons[i].text,key:JSON.rows[ii][keyIndex].text}*/
						var argcobj={};
						argcobj.key=JSON.rows[ii][keyIndex].text;
						argcobj.value=op.buttons[i].text;
						argcobj.rowIndex=ii;
						argcobj.JSON = This.backJSON;
						
						op.buttons[i].callback.call(this,argcobj);
						}
					})(i);
				}
			}
		}/* end if */
	}

}/*end function setOP */

wList.prototype.setPageRange=function(recordcount){
	/*this.pageindex从1开始,this.pagecnt是总共显示几页*/
	var i=0,curPage=1;/*存储当前页码*/
	var page='<div id="page{n}" title="{pageIndex}" class="page" style="cursor:pointer;float:left;width:60px;height:20px;margin-top:19px;line-height:20px;text-align:center;margin-left:3px;">{page}</div>';
	var tmp = "",nextPage="",prevPage="";
	var gotodiv='<div style="width:180px;height:20px;float:left;margin-top:10px;">跳转到第<input id="txtPage" type="text" value="2" style="width:30px;height:15px;border:1px solid #ccc;text-align:center;" />页&nbsp;<span id="btnGoto" style="cursor:pointer;color:#777;border:1px solid #aaa;background-color:#eee">&nbsp;确定&nbsp;</span></div>';
	var pagetip='<div style="float:right;margin-left:5px;height:20px;line-height:20px;margin-top:19px;">共{cnt}记录&nbsp;当前是第{n}页&nbsp;共{page}页</div>';
	var pi=null;
	var This=this;
	var pagesize=this.options.pagesize||10;
	var pagecount = parseInt(recordcount/pagesize)+1;
	if(recordcount%pagesize==0)
	{
		pagecount = pagecount-1;
	}
	this.pagediv.style.width = this.table.clientWidth + "px";
	this.pagediv.style.height="40px";
	this.pagediv.style.lineHeight="40px";
	
	if(recordcount<=this.options.pagesize)
	{
		$(this.parent + " #pagediv").css("display","none");
		return;
	}
	curPage = this.pageindex;
	
	this.pageFE="";
	if(this.pageindex>pagecount)/*如果是最后一页 */
	{
		/*this.pageindex = pagecount;*/
		this.pageFE="end";
	}
	
	if(this.pageindex<=1)/*如果是最后一页 */
	{
		this.pageindex = 2;
		curPage=1;
		this.pageFE="front";
	}
	
	prevPage = page.replace("{page}","上一页");
	if(this.pageDirect=="next")
	{
		prevPage = prevPage.replace("{pageIndex}",this.pageindex-2);
	}
	else
	{
		prevPage = prevPage.replace("{pageIndex}",this.pageindex-1);
	}
	prevPage = prevPage.replace("{n}",1);
	
	nextPage = page.replace("{page}","下一页");
	if(this.pageFE=="front"||this.pageDirect=="next")
	{
		nextPage = nextPage.replace("{pageIndex}",this.pageindex);
	}
	else
	{
		nextPage = nextPage.replace("{pageIndex}",this.pageindex+1);
	}
	nextPage = nextPage.replace("{n}",2);
	
	tmp = tmp + prevPage + nextPage + gotodiv;

	pagetip = pagetip.replace("{cnt}", recordcount);
	pagetip = pagetip.replace("{page}", pagecount);
	
	pagetip = pagetip.replace("{n}", this.pageDirect=="next"?curPage-1:curPage);
	
	this.pagediv.innerHTML='<div style="float:right;width:auto;">'+tmp+'<div style="float:right;width:2px;">&nbsp;</div>'+'</div>'+pagetip;
	
	$(this.parent + " #txtPage")[0].value="";
	
	if(this.pageFE=="front")
	{
		$(this.parent+" #page1").css("display","none");
	}
	if(this.pageFE=="end")
	{
		$(this.parent+" #page2").css("display","none");
	}
	
	$(this.parent + " .page").click(function(o){
		if(This.maskdiv.style.display=="block")
		{
			return;
		}
		pi = parseInt(o.target.title);
		if(o.target.id=="page1")
		{
			This.pageindex=parseInt(pi);
			This.pageDirect = "prev";
			This.curPageIndex=pi;
		}
		else
		{
			This.pageindex=parseInt(pi)+1;
			This.pageDirect = "next";
			This.existPageIndex.push(pi);
			This.curPageIndex=pi;
		}
		This.No = (parseInt(pi)-1)*pagesize+1;
		This.ajaxData(pi);
	});
	$(this.parent + " #btnGoto").click(function(o){
		if(This.maskdiv.style.display=="block")
		{
			return;
		}		
		pi = parseInt($(This.parent + " #txtPage")[0].value);
		if(pi.toString()=="NaN"){$(This.parent + " #txtPage")[0].value="";return;}
		if(pi>pagecount){$(This.parent + " #txtPage")[0].value="";return;}
		if(pi<=0){return;}
		This.pageindex=parseInt(pi)+1;
		This.pageDirect = "next";
		This.No = (pi-1)*pagesize+1;
		
		/*把this.curPageIndex存储existPageIndex[]里*/
		for(i=0;i<This.existPageIndex.length;i++)
		{
			if(This.existPageIndex[i]==this.curPageIndex)
			{
				break;
			}
		}
		if(i>=This.existPageIndex.length)
		{
			This.existPageIndex.push(this.curPageIndex);
			This.curPageIndex=pi;
		}
		This.ajaxData(pi);
	});
	
};/*end function setPageRange*/

/*-----------------------
数字分页，有BUG暂时不使用
wList.prototype.setPageRange=function(recordcount){
	var i=0;
	var page='<div id="page{n}" class="page" style="cursor:pointer;float:left;border:1px solid #dddddd;width:20px;height:20px;margin-top:19px;line-height:20px;text-align:center;margin-left:3px;">{page}</div>';
	var pageH='<div style="float:left;border:1px solid #dddddd;width:20px;height:20px;margin-top:19px;line-height:20px;text-align:center;margin-left:3px;background-color:#f1f1f1;color:#fa7a0f">{page}</div>';
	var tmp = "",pagetmp="";
	var pagetip='<div style="float:right;margin-left:5px;height:20px;line-height:20px;margin-top:19px;">共{cnt}记录</div>';
	var lastpage="";
	var pi=null;
	var This=this;
	var pagesize=this.options.pagesize||10;
	var pagecount = parseInt(recordcount/pagesize)+1;
	this.pagediv.style.width = this.table.clientWidth + "px";
	this.pagediv.style.height="40px";
	this.pagediv.style.lineHeight="40px";
	
	if(recordcount<=this.options.pagesize){return;}
		
	if(pagecount>=this.pagecnt) *如果页数大于显示范围的页数(this.pagecnt),则显示"..." 
	{
		if(this.pageindex==pagecount)
		{
			pagetmp = pageH.replace("{n}",pagecount);
		}
		else
		{
			pagetmp = page.replace("{n}",pagecount);
		}
		lastpage = pagetmp.replace("{page}",pagecount);
		lastpage = '<div style="float:left;margin-top:19px;height:20px;line-height:25px;">&nbsp;...&nbsp;</div>' + lastpage;
	}
	else
	{
		pagetmp = page.replace("{n}",pagecount);
		lastpage = pagetmp.replace("{page}",pagecount);
		lastpage = '<div style="float:left;margin-top:19px;height:20px;line-height:25px;"></div>';
	}
	 
	if(this.pageindex==5)
	{
		this.pageBase++;
	}
	for(i=this.pageBase+1;i<=this.pageBase+this.pagecnt;i++)
	{
		if(this.pageindex==i)
		{
			tmp = tmp + pageH.replace("{page}",i);
		}
		else
		{
			pagetmp = page.replace("{n}",i);
			tmp = tmp + pagetmp.replace("{page}",i);
		}
	}
	if(this.pageindexold<this.pageindex&&this.pageindex>=4&&this.pageindex<pagecount)
	{
		this.pageBase++;
		this.pageindexold = this.pageindex;
	}
	else
	{
		this.pageBase-=2;
		if(this.pageBase<1){this.pageBase=0;}
		this.pageindexold = this.pageindex;
	}
	
	tmp = tmp + lastpage;
	this.pagediv.innerHTML='<div style="float:right;width:auto;">'+tmp+'<div style="float:right;width:2px;">&nbsp;</div>'+'</div>'+pagetip.replace("{cnt}", recordcount);
	$(this.parent + " .page").click(function(o){
		pi = o.target.id.replace("page","");
		This.pageindex=parseInt(pi);
		This.No = (This.pageindex-1)*pagesize+1;
		This.ajaxData(pi);
	});
};
-------此数字分页暂不用 end----------------*/

/*
 异步交互分页去数据
 加载完数据后调用success回调函数
*/
wList.prototype.ajaxData=function(index){
	var url = this.url;
	var This = this;
	this.deleteAllData();
	if(index==undefined)index=1;
	this.maskdiv.style.display="block";
	
	$.ajax({
		url:url, 
		type:'GET', 
		dataType:'text', 
		data:{page:index,rows:This.options.pagesize,rand:Math.random()},
		success:function(data){
			var json=eval("("+data+")");
			This.initGotoPageParam();
			This.setData(json);
			setTimeout(function(){This.maskdiv.style.display="none";},300);
			if(This.options.success!=undefined)
			{
				This.options.success.call(This,json);
			}
		},
		error:function(){
			if(This.options.error!=undefined)
			{
				This.maskdiv.style.display="none";
				This.options.error.call(This,json);
			}
			else
			{
				/*alert("失败");*/
				This.maskdiv.style.display="none";
			}
		}
     });	
}/*end function ajax */

wList.prototype.deleteAllData=function(){
	var i=0;
	var t;
	for(i=0;i<this.rowcount;i++)
	{
		t=this.table.deleteRow(-1);
	}
}

wList.prototype.exChangeData=function(JSON){/*把key:value转换成内部使用的属性和方法*/
	var i;
	var innerJSON={};
	innerJSON.total=JSON.total;
	innerJSON.rows=[];
	var row=[];
	var obj;
	for(i=0;i<JSON.rows.length;i++)
	{
		row=[];
		obj=null;
		for(v in JSON.rows[i])
		{
			obj = new Object();
			obj.name=v;
			obj.text=JSON.rows[i][v];
			row.push(obj);
		}
		innerJSON.rows.push(row);
	}
	this.backJSON = JSON;
	this.backCheckValues = JSON.check;
	return innerJSON;
}

wList.prototype.refresh = function(){
	if(this.curPageIndex==1)
	{
		this.No=1;
		this.pageindex = 1;
		this.pageDirect="prev";		
		this.ajaxData(1);
		return;
	}
	if(this.pageDirect=="next")
	{
		this.ajaxData(this.pageindex-1);
		this.No=this.options.pagesize*(this.pageindex-2)+1;
	}
	else
	{
		this.ajaxData(this.pageindex);
		this.No=this.options.pagesize*(this.pageindex-1)+1;
	}
}

/* **************************************
根据后端传来的JSON数据格式，判断是否有 "check":["主键1","主键2","主键3",...]
{
    "rows":
          [
          { "created_time":"2013-11-29"  ,  "desc":"desc"  ,  "id":"8a98c1"  ,  "location":"3"  ,  "name":"22214:30\u4FEE\u65395" },
          { "created_time":"2013-11-29"  ,  "desc":"desc"  ,  "id":"8a98c2"  ,  "location":"321"  ,  "name":"\u6D4B\u8BD56" },
          { "created_time":"2013-12-25"  ,  "desc":"desc"  ,  "id":"8a98c"  ,  "location":"2"  ,  "name":"www" }
          ],
   "total":29,
   "check":["主键值1","主键值2","主键值3",...]
}
*/
wList.prototype.setCheck = function(JSON){
	var i,j;
	var isAdd=false;/*如果this.checkValues里没有值，就push()一个到this.checkValues里*/
	var This = this;
	var key="";
	var checkar=[];
	var check_len,checkVal_len,isHave=false;/*check_len存储后端check数组长度,checkVal_len存储checkValues数组长度*/
	if(JSON.check!=undefined)
	{
		checkar = JSON.check;
		$(this.parent + " .wcheck").each(function(index){
			if(This.haveNo)
			{
				if(this.parentNode.nextSibling.nextSibling.children[0].innerText!=undefined)
				{
					key = this.parentNode.nextSibling.nextSibling.children[0].innerText;
				}
				else
				{
					key = this.parentNode.nextSibling.nextSibling.children[0].textContent;
				}
			}
			else
			{
				if(this.parentNode.nextSibling.nextSibling.children[0].innerText!=undefined)
				{
					key = this.parentNode.nextSibling.children[0].innerText;
				}
				else
				{
					key = this.parentNode.nextSibling.children[0].textContent;
				}
			}
			for(i=0;i<checkar.length;i++)
			{
				if(key==checkar[i])
				{
					this.checked=true;
					if(This.options.keepcheck)
					{
						for(j=0;j<This.checkValues.length;j++)
						{
							if(key==This.checkValues[j].key)
							{
								This.checkValues[j].check=true;
							}
						}
					}
					else
					{
						This.checkValues[index].key=key;
						This.checkValues[index].check=true;
						break;
					}
				}
			}
		});
		
		/*如果是当前页的复选框都选中，全选的复选框应该选中 (此功能代码没有写)*/
		this.SetBackCheckValues();
	}/*endif check*/
}/* end function setCheck */

wList.prototype.SetBackCheckValues = function(){
	/*设置backCheckValues[]数组就是后端"check:['key1','key1',]"的值 ，功能：并除去checkValues[]里的值*/
	var i,j,k,bool=false/*bool是是否要删除backCheckValues[]里的元素*/;
	var json = this.backJSON;
	var check_len = this.backJSON.check.length;
	var checkVal_len = this.checkValues.length;
	var ar=[];/*存储待删除的字符串即key值*/
	/*alert(JSON.stringify(this.checkValues));*/
	for(i=0;i<check_len;i++)
	{
		bool=false;
		isHave=false;
		for(j=0;j<checkVal_len;j++)
		{
			if(json.check[i]==this.checkValues[j].key)
			{
				ar.push(json.check[i]);
				break;
			}
		}
	}

	if(this.backCheckValues.length>1)
	{
		for(i=0;i<ar.length;i++)
		{
			bool=false;
			for(j=0;j<this.backCheckValues.length-1;j++)
			{
				if(this.backCheckValues[j]==ar[i])
				{
					this.backCheckValues.splice(j,1);
					break;
				}
			}
		}
	}
	else
	{
		this.backCheckValues.length=0;
	}
	
}/*end function SetBackCheckValues */

wList.prototype.selectCheckBox=function(rowIndex){
	$(this.parent + " #wcheck"+rowIndex)[0].checked=true;
	this.checkValues[this.checkValues.length-1].check=true;
}/* end function selectCheckBox */

wList.prototype.hiddenColumns = function(){
	$(this.parent + " .hidden").hide();
}/*end function hiddenColumns*/

wList.prototype.initGotoPageParam=function(){
	if(this.options.keepcheck!=undefined&&this.options.keepcheck==true)/*外界传进来的 keepcheck表示点击翻页的后保留this.checkValues*/
	{
		/*不做处理*/
	}
	else
	{
		this.checkValues.length=0;
	}
	this.isCheck=false;
	if($(this.parent + " #wMainCK")[0]!=undefined)
	{
		$(this.parent + " #wMainCK")[0].checked=false;
	}
}/*end function initGotoPageParam*/
/**
 * 判断是否为array
 */
function isArray(obj){
	return Object.prototype.toString.call(obj) === '[object Array]';
}
/**
 * <h4>增加合并表头(列合并）</h4>
 * <p>使用方式 在options中增加 multiHead 字段,值为object数组,object中start为列需要合并的起始索引,end为需要合并的终止索引,text为合并后显示的值。</br>
 * 例如:multiHead:[{start:0,end:2,text:'test'},{start:2,end:3,text:'test1'},{start:4,end:5,text:'test2'}]</p>
 * <pre>注意：
 * 1、start的值需小于end的值
 * 2、数组中最后一个数的end如果大于所有列的值，将取列的值。
 * 4、数组中最后一个数的end值如果小于列的最后一列的索引，将生成最后列索引减去数组最后一个数的end的值的个数的列。（最后数组end为7，最后一列的索引为10，那么将会在执行玩最后一个数组数的列合并后，生成10-7=3个列。）
 * 5、相邻两个数据中后一个数据start减去前一个数据的end 值如果大于0，将生成无合并列的列（单列）
 * 6、数组第一个值的start如果非0，那么从左到右将会生成 该start值减去0个数的单列。
 * </pre>
 * <i>使用方式：business/group/index.html页面中第256行</i>
 */
wList.prototype.addMultiHead =function(){
	var headers;
	if(this.options.multiHead && isArray(this.options.multiHead)){
		headers=this.options.multiHead;
		/*TODO 区间重叠判断*/
		for(var i =0;i <headers.length;i++){
				if(headers[i].start>=headers[i].end){
					throw RangeError('multiHead index '+i+' start value must be < end value');
				}
				if((i+1)<headers.length){
					 var seqFirstMax,
					 	 seqSecondMin;
					 var first = headers[i],
					 	 second = headers[i+1];
					 seqFirstMax = first.end;
					 seqSecondMin = second.start;
					 if(seqSecondMin - seqFirstMax <=0){
						 throw RangeError('multiHead index '+i+' and index '+(i+1)+' has values in a same interregional ');
					 }
				}
		}
	}else{
		return;
	}
	/*构造无合并列*/
	var bulidTD  = function(row,count){
		for(var e = 0 ; e <count ; e++){
			var td = row.insertCell();
				td.className = 'tdheader';
				td.style.textAlign = 'center';
				td.style.border = 'none';
				td.innerHTML = '&nbsp;';
		}
	}
	var columnsLength = this.table.rows[0].cells.length;
	var row = this.table.insertRow(0);
	/*定义隐藏列个数计数*/
	var unVisableCount=0;
	/*获取隐藏列数*/
	for(var i =0;i<columnsLength;i++){
		if(this.table.rows[1].cells[i].style.display == 'none'){
			unVisableCount++;
		}
	}
	/*获取可视列数*/
	var visableCount = columnsLength - unVisableCount;
	/*构造td*/
	for(var i =0,j=0;i<visableCount;j++){
			if(j<headers.length){/*确保合并列存在*/
				var start = headers[j].start;
				var end   = headers[j].end;
				var text  = headers[j].text;
				var colCount = end - start ;
				if(j==0&&start>0){
					var startCount = start - 0;
					bulidTD(row,startCount);
					i += startCount;
				}
				var cell = row.insertCell();
				cell.colSpan = colCount;
				cell.className = 'tdheader';
				cell.style.textAlign = 'center';
				cell.style.border='none';
				cell.style.borderBottom = '1px solid #E0E0E0';
				cell.innerHTML = text;
				i+= colCount;
				if(j == headers.length-1){
					/*合并列头完毕，后面剩余的列*/
					var endCount = visableCount - i;
					if(endCount>0){
						bulidTD(row,endCount);
						i += endCount;
					}
				}
				
				/*开始构造中间无合并的列*/ 
				if(i<=visableCount-1 ){
					var nextStart = headers[j+1];
					if(nextStart){
						var tdCount = nextStart.start - end ;
						bulidTD(row,tdCount);
						i+= tdCount;
					}
					
				}
			}
		
	}
}/*addMultiHead*/

wList.prototype.isLowChrome = function(){
	var ua = navigator.userAgent;
	var reg = /Chrome\/\d{2,}/;
	var r = reg.exec(ua);
	if(r!=null)
	{
		r = parseInt(r[0].replace("Chrome/",""));/*chrome版本号<36 table列是倒着生成的，chrome版本号>=36 table列是正的生成的与火狐IE一样*/
	}
	else
	{
		r=0;
	}
	if((window.navigator.userAgent.indexOf("WebKit")>0)&&(r<36)) 
	{
		return true;
	}
	else
	{
		return false;
	}
}/*end function isLowChrome*/


